<?PHP
class content
{
	
	
	var $data;
	var $input;
	var $output;
	var $where;
	var $options;
	var $display;
	var $piece;
	var $everything=FALSE;
	var $plain=FALSE;
	var $shows;
	

	//list al content types
	function content()
	{
	
		//if($_SESSION['user_level'] > 8)
		if(FALSE)
		{
			
			$w[0]='owner';
			$w[1]=$_SESSION['user_id'];
			$w[2]='=';
			$where[]=$w;
		}
		//$this->data=get_sql('content','id',$where,NULL);
	}
	function process_content_category($mode)
	{
		$this->data=get_sql('content_category','id',$where,NULL);
	}
	
	
	function get_content()
	{
		
		global $conf;
		$where=process_input($this->where);
		if(!$conf->test_content && !$this->everything)
		{

			$w[0]='status';
			$w[1]='enabled';
			$w[2]='=';
			$where[]=$w;
			

		}

		
		if(!$conf->test_content && $this->input['LIMITDATE']=='true')
		{

			$w[0]='start';
			$w[1]=date("Y-m-d");
			$w[2]='<=';
			$where[]=$w;
			
			$w[0]='end';
			$w[1]=date("Y-m-d");
			$w[2]='>=';
			$where[]=$w;
		}
		
		if(!$this->plain)
		{
			$ij[0]=TRUE;
			$ij[1]='content_image';
			$ij[2][0]='thumb';
			$ij[2][1]='image';
			$ij[3][0]='id';
			$ij[3][1]='id';
		}

		
		mp('getting content');
		$this->data=get_sql('content','id',$where,$this->options,$ij);
		$this->piece=current($this->data);
	}
	function get_category_content()
	{
		global $conf;
	
		if(!$conf->test_content)
		{
			$w[0]='status';
			$w[1]='enabled';
			$w[2]='=';
			$where[]=$w;
			
				$w[0]='start';
				$w[1]=date("Y-m-d");
				$w[2]='<=';
				$where[]=$w;

		
		}
				$w[0]='end';
				$w[1]=date("Y-m-d");
				$w[2]='>=';
				$where[]=$w;
		
		if(!empty($this->input['category']))
		{
			$w[0]='category';
			$w[1]=$this->input['category'];
			$w[2]='=';
			$where[]=$w;
		}
		

		
		$w[0]='type';
		$w[1]='video';
		$w[2]='=';
		$where[]=$w;
		
		if($this->input['order_by_date'])
		{
				$this->options['order'][0]='start';
				$this->options['order'][1]='desc';
		}
		else
		{
				$this->options['order'][0]='weight';
				$this->options['order'][1]='desc';
		}
	
	
		if($this->input['limit']==true)
		{
			$this->options['limit'][0]='0';
			$this->options['limit'][1]='3';	
		}
		
		
		
		$ij[0]=TRUE;
		$ij[1]='content_image';
		$ij[2][0]='thumb';
		$ij[2][1]='image';
		$ij[3][0]='id';
		$ij[3][1]='id';
		mp('getting Category content');
		$this->data=get_sql('content','id',$where,$this->options,$ij);
		
	}
	
	
	function get_content_extras()
	{
		mp("get addons");
		$m=$this->piece;
		$w[0]='id';
		$w[1]=$m['id'];
		$w[2]='=';	
		$where[]=$w;
		
		//$f=current(get_sql('content_image','id',$where,NULL));
		$i=current(get_sql('content_video','id',$where,NULL));
		//$j=current(get_sql('content_access','id',$where,NULL));
		$e=array_merge((array)$i,(array)$m);
	
		
		$this->piece=$e;
	}
	function get_content_addons()
	{
		mp("get addons");
		$m=current($this->data);
		$w[0]='id';
		$w[1]=$m['id'];
		$w[2]='=';	
		$where[]=$w;
		
		//$f=current(get_sql('content_image','id',$where,NULL));
		$i=current(get_sql('content_video','id',$where,NULL));
		//$j=current(get_sql('content_access','id',$where,NULL));
		$e=array_merge((array)$i,(array)$m,(array)$j);
	
		
		$this->data=$e;
	}
	
	function get_all_content_addons()
	{
		foreach($this->data as $x)
		{
		//	$m=current($this->data);
			$w[0]='id';
			$w[1]=$x['id'];
			$w[2]='=';	
			$where[]=$w;

		//	$f=current(get_sql('content_image','id',$where,NULL));
			$i=current(get_sql('content_video','id',$where,NULL));
			$j=current(get_sql('content_access','id',$where,NULL));
			$e=array_merge((array)$i,(array)$m,(array)$j);
			
		}
		
		
		$this->data[]=$e;
	}
	
	function get_metadata($piece,$mode)
	{
		if(empty($piece)){
			$m=$this->data;
		
		
			foreach($m as $x)
			{
			
				$w[0]='id';
				$w[1]=$x['id'];
				$w[2]='=';	
				$where[0]=$w;
			
		
				$f=current(get_sql('content_image','id',$where,NULL));
				$i=current(get_sql('content_video','id',$where,NULL));
				$j=current(get_sql('content_access','id',$where,NULL));
				$e=array_merge((array)$f,(array)$i,(array)$x,(array)$j);
				$z[]=$e;
			}
		
			$this->data=$z;
	  	}
		else
		{
			if(empty($mode)){
				$w[0]='id';
				$w[1]=$piece['id'];
				$w[2]='=';	
				$where[0]=$w;
				$i=current(get_sql('content_meta','id',$where,NULL));	
				$e=array_merge((array)$piece,(array)$i);
				$this->piece=$e;
			}
			else{
				$w[0]='id';
				$w[1]=$piece['id'];
				$w[2]='=';	
				$where[0]=$w;
				$i=current(get_sql('content_meta','id',$where,NULL));	
				return $i;
					
			}
		}
	}
	
	function process_images()
	{
		foreach($this->data as $x)
		{
		
			if(empty($x['thumb']))
			{
				$x['thumb']='thumbnail-default.jpg';
			}
			
			$m[]=$x;
			
		}
		
		$this->data=$m;
	}
	function get_h1($mode,$count)
	{
		global $conf;
		switch($mode)
		{
			case 0:	
			
			
		  	$sql="SELECT *
			FROM  content as a, content_image as b, content_post as c WHERE a.id = b.id and a.id = c.id 
			ORDER BY c.h1 ASC LIMIT 0,$count";
			break;
			
			default:

			$sql="SELECT *
			FROM  content as a, content_image as b, content_post as c WHERE a.id = b.id and a.id = c.id 
			ORDER BY c.d1 ASC LIMIT 0,$count";
			break;
		}
		$result = mysql_query($sql); 
		while($row=mysql_fetch_array($result))
		{	
			$akey=$row['id'];
			$arr[$akey]=$row;

			//print_r($row);
			
			$m[]=$row;
		}
		
		return $arr;
	}
	
	function get_posts($cat,$sort)
	{
		if(empty($cat)){	
	  		$sql="SELECT * FROM  content as a, content_image as b, content_post as c WHERE a.id = b.id and a.id = c.id"; 
   	}  
   	else
		{
			$sql="SELECT a.id, a.title, a.secondary, a.start, b.image, b.thumb, c.post_type FROM  content as a, content_image as b, content_post as c WHERE a.id = b.id and a.id = c.id and a.category=$cat";
	
		}
		if(empty($sort))
      	$sql .= " ORDER BY c.post_type,c.w ASC";
		else
		{
		  switch($sort){	
		  case 'date':
		  		$sql .= " ORDER BY a.start DESC";
		  break;
		  case 'dateweight':
				$sql .= " ORDER BY c.w ASC,a.start DESC";
		  break;	
		  }
		}
		$result = mysql_query($sql); 
		while($row=mysql_fetch_array($result))
		{	
			$akey=$row['id'];
			$arr[$akey]=$row;

			//print_r($row);
			
			$m[]=$row;
		}
		
		return $arr;
	}
	function get_category_items($category)
	{
		
		
		
		$today=date("Y-m-d");
		$sql="SELECT content.*, content_image.image, content_image.thumb
		FROM  `content` 
		INNER JOIN `content_image` ON content.id = content_image.id
		WHERE";
		
		if(!empty($category))
		{
			
		
		$sql.="(`category` IN (SELECT `id` FROM `content_category` WHERE `parent` = '".$category."') OR `category`='".$category."')
			AND";	
		}
		
		
		$sql.=" `status` =  'enabled' 
				AND `end` >=  '".$today."'
				AND `start` <=  '".$today."'
		AND (`type`='post' OR `type`='video')
	
		ORDER BY `weight` DESC, `start` DESC
		LIMIT 0 , 50";
	
		$result = mysql_query($sql); 
		while($row=mysql_fetch_array($result))
		{	
			$akey=$row['id'];
			$arr[$akey]=$row;

			//print_r($row);
			
			$m[]=$row;
		}
		
		return $arr;
		
	}
	
	function process_meta()
	{
		global $conf;
	
		$where=process_input($this->where);
		$data=get_sql('content_meta','id',$where,NULL);
		$meta=current($data);
		rp($meta);
		if(!empty($meta['meta_title']))
		{
			$conf->meta['Title']=$meta['meta_title'];
		
		}
		else
		{
			$conf->meta['Title']=$this->data['title'];
		}

		if(!empty($this->data['thumb']))
		{
				$conf->a['img-src']=ap($this->data['image']);
		}
		else
		{
			$conf->a['img-src']=ap('pinoytuner-icon.jpg');
		}
	
		
		if(!empty($meta['meta_searchtitle']))
		{
			$conf->meta['SearchTitle']=$meta['meta_searchtitle'];
		
		}
		else
		{
			$conf->meta['SearchTitle']=$this->data['title'];
		}

		if(!empty($meta['meta_description']))
		{
			$conf->meta['Description']=$meta['meta_description'];
		
		}
		else
		{
			$conf->meta['Description']=strip_tags($this->data['secondary']);
		}
		
		if(!empty($meta['meta_searchdescription']))
		{
			$conf->meta['SearchDescription']=$meta['meta_searchdescription'];
		
		}
		else
		{
			$conf->meta['SearchDescription']=strip_tags($this->data['secondary']);
		}
		
		if(!empty($meta['meta_keywords']))
		{
			$conf->meta['Keywords']=$meta['meta_keywords'];
		
		}
	
	}
function get_shows()
	{
		mp('grab items');
		$sql = "select * from content as a, content_video as b, content_image as c where a.id=b.id and a.id=c.id and b.playlist=0 and a.type='video' and b.show>0 ORDER BY a.start DESC";
		$result = mysql_query($sql);
		$r = mysql_fetch_array($result);
		$rs = "";
		do
		{
			
		 	$rs[] = $r;		
		}while($r = mysql_fetch_array($result));
		$this->shows = $rs;

	}
function get_todays_show()
	{
		$today= date("Y-m-d");
		foreach($this->shows as $x){
			$date_sub = substr($x['start'],0,10);			
			if($x['type']=='video' && $today==$date_sub)

				return $x;
		}
	}
	function get_latest_video($todayshow,$videos)
	{ 

		$today= date("Y-m-d");
		if(empty($videos))
			$videos = $this->items;	
		
		foreach($videos as $x){		
			$date_sub = substr($x['start'],0,10);		
			if($x['type']=='video' && $today==$date_sub && $x['id']!=$todayshow['id'])
				return $x;
		}
	}
	function get_videos($cat)
	{
		mp('grab items');
		global $conf;
		unset($this->where);
		$this->where['category']=$cat;
		$this->where['type']='video';
		$this->videos=$this->get_category_items($cat);
		$this->get_shows();
	}

}
?>
